<template>
  <div class="view_container">
    <el-card class="view_header">
      <i class="el-icon-search"></i>
      <span class="view_header_title">筛选搜索</span>
      <el-button class="fr ml20" size="small" type="primary" @click="fetchData"
        >查询搜索</el-button
      >
      <el-button class="fr" size="small" @click="reset">重置</el-button>
      <el-form
        :inline="true"
        :model="formInline"
        class="inline_search"
        size="small"
      >
        <el-form-item label="角色名称:">
          <el-input
            v-model="formInline.keyword"
            placeholder="请输入角色名称"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="view_list">
      <i class="el-icon-tickets"></i>
      <span class="view_header_title">数据列表</span>
      <el-button
        class="fr ml20"
        size="small"
        type="primary"
        v-permission="'addRole'"
        @click="edit"
        >添加</el-button
      >
    </el-card>
    <el-table :data="tableData" border style="width: 100%" size="small">
      <el-table-column
        align="center"
        header-align="center"
        prop="id"
        label="编号"
        width="50"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="name"
        label="角色名称"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="description"
        label="描述"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="adminCount"
        label="用户数"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="添加时间"
        width="170"
      >
        <template slot-scope="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="是否启用">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="statusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="操作"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            v-permission="'allocMenu'"
            size="small"
            @click="jumpAllocMenu(scope.row)"
            >分配菜单</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="jumpAllocRes(scope.row)"
            v-permission="'allocResource'"
            >分配资源</el-button
          >
          <el-button
            type="text"
            v-permission="'editRole'"
            size="small"
            @click="edit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="del(scope.row)" v-permission="'delRole'"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pag_content"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <AddRole :visible="roleVisible" :form="formData" @close="closeRoleDialog" />
  </div>
</template>

<script>
import { getRoleList, updateUserStatus, delRoleLists } from "@/request/api";
import { formatDate } from "@/util/util";
import AddRole from "./children/addRole";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      roleVisible: false,
      formData: {},
    };
  },
  components: {
    AddRole,
  },
  computed: {
    permission() {
      return this.$store.getters.permission;
    },
  },
  methods: {
    formatDate,
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    fetchData() {
      getRoleList({
        keyword: this.formInline.keyword,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    statusChange(row) {
      console.log(row);
      updateUserStatus(row.id, {
        status: row.status,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("状态修改成功");
        }
      });
    },
    closeRoleDialog(reload) {
      this.roleVisible = false;
      this.formData = {};
      if (reload === "reload") {
        this.fetchData();
      }
    },
    // 分配菜单
    jumpAllocMenu(row) {
      this.$router.push({
        path: "/ums/allocMenu",
        query: {
          id: row.id,
        },
      });
    },
    jumpAllocRes(row) {
      this.$router.push({
        path: "/ums/allocResource",
        query: {
          id: row.id,
        },
      });
    },
    edit(row) {
      if (row.id) {
        this.formData = { ...row };
      } else {
        this.formData = { status: 1 };
      }
      this.roleVisible = true;
    },
    del(row) {
      this.$confirm(`此操作将删除当前用户, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delRoleLists({ ids: row.id }).then((res) => {
            if (res.code == 200) {
              this.fetchData();
              this.$message.success(res.message);
            }
          });
        })
        .catch(() => {});
    },
    reset() {
      this.formInline.keyword = "";
    },
  },
  created() {
    this.fetchData();
  },
};
</script>

<style lang="less" scoped>
.view_header_title {
  padding-left: 10px;
  font-size: 14px;
}
.inline_search {
  padding: 20px 40px 0;
}
.view_list {
  margin: 20px 0;
}
.fr {
  float: right;
}
.ml20 {
  margin-left: 20px;
}
.pag_content {
  float: right;
  margin: 20px 0;
}
</style>